package com.meihua.pickerviewlibrary.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * The simple Array wheel adapter
 */
public class ArrayWheelAdapter implements WheelAdapter {

    /**
     * The default items length
     */
    public static final int DEFAULT_LENGTH = 4;

    // items
    private ArrayList<Map<String, String>> items;
    // length
    private int length;

    /**
     * Constructor
     *
     * @param items  the items
     * @param length the max items length
     */
    public ArrayWheelAdapter(ArrayList<Map<String, String>> items, int length) {
        this.items = items;
        this.length = length;
    }

    /**
     * Contructor
     *
     * @param items the items
     */
    public ArrayWheelAdapter(ArrayList<Map<String, String>> items) {
        this(items, DEFAULT_LENGTH);
    }

    @Override
    public Map<String, String> getItem(int index) {
        if (index >= 0 && index < items.size()) {
            return items.get(index);
        }
        return new HashMap<>();
    }

    @Override
    public int indexOf(Object o) {
        HashMap m = (HashMap) o;
        for (int i = 0; i < items.size(); i++) {
            if (m.get("area_name").equals(items.get(i).get("area_name")) && m.get("area_id").equals(items.get(i).get("area_id"))) {
                return i;
            }
        }

        return -1;
    }


    @Override
    public int getItemsCount() {
        return items.size();
    }

//    public int indexOf(Object o) {
//        return items.indexOf(o);
//    }

}
